<?php

namespace App\Services\SpiderContent;

use Carbon\Carbon;
use App\Models\User;
use App\Models\News;
use App\Jobs\RewardFirstParentJob;
use Illuminate\Support\Facades\Log;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Database\Eloquent\Collection;
use Sunra\PhpSimple\HtmlDomParser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use DOMDocument;


class SpiderHuxiaoDeepContent
{

    public function execute($baseUrl)
    {
    	ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    	
    	//$url = "https://www.baidu.com";
    	$html = file_get_contents($baseUrl);

    	//dd($html);

    	//$html = '<a href="aaaa"><div class="content-item"><div>测试</div></div></a>';
    	$dom = new DOMDocument();
    	

    	libxml_use_internal_errors(true);
    	//$html = htmlspecialchars($html);
    	
		
        $dom->loadHTML('<?xml encoding="UTF-8">' . $html);
      
        $elements = $dom->getElementsByTagName('div');

     	//$html = HtmlDomParser::str_get_html($html);

     	//dd($elements->nodeValue);

     	$data = [];
     	$i = 0;

     	$briefMatch = "/\"brief_id\":\d+,/";
     	$imgMatch = "/src=\".*?\"/";

     	$hoursMatch = "/\d+小时前/";
     	$MinutsMatch = "/\d+分钟前/";

        foreach($elements as $key=>$val){
        	$url = "";

    		if (strpos($val->getAttribute('class'), 'article-item-wrap') !== false) {
    			

    			$content =   $dom->saveHTML($val);

    			if(strstr($content,'分钟前')){
    				$data[$i]= ["images"=>"","brief"=>'',"content_cn"=>"","title_cn"=>"","images"=>"",'source'=>'huxiu',"created_at"=>Carbon::now(),"url"=>"","started_at"=>Carbon::now()];
    				
    				/*$timeMatchContent  = preg_match($hoursMatch,$content,$match);

    	 			if(!empty($match)){
	    	 			preg_match('/\d+/',$match[0],$times);
	    	 			if(!empty($times)){

	    	 				$matchTime = $times[0];
	    	 				$data[$i]['started_at'] = Carbon::now()->subHours($matchTime);

	    	 			}

	    	 		}else{*/
	    	 			preg_match($MinutsMatch,$content,$match);
	    	 			if(!empty($match)){
	    	 				preg_match('/\d+/',$match[0],$times);
	    	 			}
	    	 			if(!empty($times)){

	    	 				$matchTime = $times[0];
	    	 				$data[$i]['started_at'] = Carbon::now()->subMinutes($matchTime);
	    	 				
	    	 			}	

	    	 		//}

    				
    				$childNodes = $val->childNodes;
	    			$childNode = $childNodes[0];
				
					if($childNode->nodeName=="a"){

						$data[$i]['url'] = $childNode->getAttribute('href');

						$getImgNodes = $childNode->childNodes;
						/*foreach($getImgNodes as $ccNode){

							if($ccNode->nodeName=="div"){
								if($ccNode->childNodes){

									foreach($ccNode->childNodes as $imageNode){

										if($imageNode->nodeName=="img"){
											$data[$i]['title_cn'] = $imageNode->getAttribute('alt');
										}	

									}
								}
							}
							
						}*/
					}

					if(!empty($data[$i]['url'])){

						//dd($data[$i]['url']);

						$itemContent = file_get_contents($data[$i]['url']);
						$contentDom = new DOMDocument();

	     		   		$contentDom->loadHTML('<?xml encoding="UTF-8">' . $itemContent);
	     		   		$imgElements =  $contentDom->getElementsByTagName('meta');
	     		   		
	     		   		foreach($imgElements as $imgElement){

	     		   			//dd($contentDom->saveHTML($imgElement));
	     		   			if (strpos($imgElement->getAttribute('property'), 'og:image') !== false) {
	     		   				$imgUrl = $imgElement->getAttribute('content');

	     		   				$data[$i]['images'] =  $imgUrl;
	     		   				//$imgArr = explode('?',$imgUrl);
	     		   				/*$imageContent = $this->getImageUrl($imgUrl);
	     		   				dd($imageContent);
	 							$newFileName = "/images/".Str::random(40) . '.jpg'; // 假设是JPG格式
								Storage::disk('oss')->put($newFileName, $imageContent);
								 
								// 你现在可以通过存储的路径访问图片
								$data[$i]['images'] = Storage::disk('oss')->path($newFileName);*/
	     		   				
	     		   			}
	     		   			
	     		   		}

	     		   		$h1Elements =  $contentDom->getElementsByTagName('h1');
	     		   		foreach($h1Elements as $h1Element){

	     		   			if (strpos($h1Element->getAttribute('class'), 'article__title') !== false) {
	     		   				$data[$i]['title_cn'] = $h1Element->textContent;
	     		   			}

	     		   			
	     		   		}

	     		   		if(strstr($data[$i]['title_cn'],'嗅之星')){

	     		   			continue;
	     		   		}

	     		   		$divElements =  $contentDom->getElementsByTagName('div');
	     		   		foreach($divElements as $divElement){

	     		   			if (strpos($divElement->getAttribute('class'), 'ai-summary__content') !== false) {
	     		   				$data[$i]['brief'] = $contentDom->saveHTML($divElement);
	     		   			}

	     		   			if (strpos($divElement->getAttribute('class'), 'article__content') !== false) {
	     		   				$data[$i]['content_cn'] = $contentDom->saveHTML($divElement);
	     		   				$data[$i]['content_cn'] = str_replace("_src","src",$data[$i]['content_cn']);


	     		   			}
	     		   			
	     		   		}

	     		   		if(strstr($data[$i]['content_cn'],'AI生成')){

	     		   			continue;
	     		   		}

	     		   		/*$iElements =  $contentDom->getElementsByTagName('i');

	     		   		foreach($iElements as $ik=>$iElement){

	     		   			if (strpos($iElement->getAttribute('class'), 'num') !== false) {
	     		   				if($ik==0){

	     		   					$data[$i]['collections'] = $contentDom->saveHTML($iElement);

	     		   				}elseif($ik==2){

	     		   					$data[$i]['likes'] = $contentDom->saveHTML($iElement);
	     		   				}
	     		   				
	     		   			}
	     		   			
	     		   		}
*/
					}


					$i++;
					sleep(1);

    			}
    			
    			
    			
    	  }

        	
        	  
		}



		DB::table('deep_news')->insert($data);
		//dd($data);
     	 
    	//dd($elements);

     	//dd(retrieve_url_contents($url));
     	//$html = file_get_contents('https://www.huxiu.com/club/1000.html?object_type=51&object_id=1');
     	//dd($html);
 
		
    }


    public function djNodes($nodes,$res)
    {
    	if($nodes->nextSibling && $nodes->nextSibling->nodeName!="h4"){

    		$res['content'] = $res['content'].$nodes->nextSibling->textContent;


    		if($nodes->nextSibling->childNodes){

    			foreach($nodes->nextSibling->childNodes as $nn){
    				if($nn->nodeName=="img"){
    					$res['image'] = $nn->getAttribute('src');
    				}
    			}
    		}
    		
    		if(isset($nodes->nextSibling->nextSibling)  && $nodes->nextSibling->nextSibling->nodeName != "h4"){

    			 $res = $this->djNodes($nodes->nextSibling,$res);
    		}
    	}

    	return  $res;
    	//dd($nodes->nextSibling->nextSibling);
    	

    }



	function getImageUrl($url) {
	    // 初始化cURL
		dd($url);
		 $user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36";
	   ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

	   $ip_long = array(
	        array('607649792', '608174079'), //36.56.0.0-36.63.255.255
	        array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
	        array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
	        array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
	        array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
	        array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
	        array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
	        array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
	        array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
	        array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
	    );
	    $rand_key = mt_rand(0, 9);
	    $ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
	//模拟http请求header头
	    $header = array("Connection: Keep-Alive","Accept:image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3","User-Agent: .$user_agent",'CLIENT-IP:'.$ip,'X-FORWARDED-FOR:'.$ip,'referer:https://www.huxiu.com/');
	   $ch = curl_init();
 	
		// 设置cURL选项
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
		//curl_setopt($ch, CURLOPT_PROXY, $ip);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // 重要，告诉cURL返回二进制内容
		curl_setopt($ch, CURLOPT_REFERER, "https://www.huxiu.com/");
		 
		 
		// 执行cURL会话
		$imageData = curl_exec($ch);
		 
		// 关闭cURL资源，并释放系统资源
		curl_close($ch);
	    return $imageData;
	}

	public function __invoke()
	{
	
		//Dinglog::text("========== refresh seckill product num start ......");

		//$baseUrl = "https://www.huxiu.com/channel/105.html";
		$baseUrl = "https://www.huxiu.com/article";
		$this->execute($baseUrl);

		//Dinglog::text("========== refresh seckill product num end ......");
	}
}
